package br.com.vm.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import br.com.vm.model.Mapa;
import br.com.vm.service.MapaService;

@Controller
public class MapaController {

	@Autowired
	private MapaService mapaService;

	@RequestMapping("/mapa/index")
	public String listMapas(Map<String, Object> map) {
		map.put("mapa", new Mapa());
		try {
			map.put("mapaList", new HashSet<Mapa>(mapaService.listarMapas()));
		} catch (Exception e) {
			map.put("mapaList", new ArrayList<Mapa>());
		}
		return "mapa";
	}

	@RequestMapping(value = "/mapa/add", method = RequestMethod.POST)
	public String addMapas(@ModelAttribute("mapa") Mapa mapa,
			BindingResult result) {
		try {
			mapaService.salvarMapa(mapa);
		} catch (Exception e) {
			// result.addError(e.getMessage());
		}
		return "redirect:/mapa/index";
	}

	@RequestMapping("/mapa/delete/{mapaId}")
	public String deleteMapa(@PathVariable("mapaId") Integer mapaId) {
		try {
			mapaService.removerMapa(mapaId);
		} catch (Exception e) {
		}
		return "redirect:/mapa/index";
	}

}
